package projects.android.sms.activities;

import java.util.ArrayList;

import projects.android.sms.R;
import projects.android.sms.adapter.SMSAdapter;
import projects.android.sms.db.DBHandle;
import projects.android.sms.encrypter.Encrypter;
import projects.android.sms.object.NumberHelper;
import projects.android.sms.object.SMS;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Activity showing a conversation between the user and a certain dialog partner.
 * 
 * @author Thomas Jaeger
 * @author Mario Kraml
 * @author Tassilo Posegga
 * @author Robert Schenkenfelder
 * 
 */
public class UserMessagesActivity extends Activity implements OnItemClickListener {

	private ListView ltDetailSms;
	private TextView tvUsername;
	private String username;
	private ArrayList<SMS> sms;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms_user_view);
		ltDetailSms = (ListView) findViewById(R.id.lt_detail_sms);

		tvUsername = (TextView) findViewById(R.id.tv_username);

		username = getIntent().getStringExtra("username");
		tvUsername.setText(NumberHelper.resolveNumberToName(getApplicationContext(), username));

		DBHandle db = DBHandle.getInstance(getApplicationContext());
		sms = db.getAllMessagesFromUser(username);
		db.close();

		ltDetailSms.setAdapter(new SMSAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, sms));
		ltDetailSms.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, final int pos, long id) {

		// Toast.makeText(getApplicationContext(), sms.get(pos).getMessage(),
		// Toast.LENGTH_SHORT).show();

		// init Dialog
		showEncryptedStringDialog(sms.get(pos));
	}

	/**
	 * Shows a Dialog with the encrypted text of the message
	 * 
	 * @param _sms
	 *            The SMS object that represents a message.
	 */
	protected void showEncryptedStringDialog(SMS _sms) {
		// init Dialog
		final Dialog dialog = new Dialog(UserMessagesActivity.this);
		dialog.setContentView(R.layout.decryptedtext_dialog);
		dialog.setTitle(NumberHelper.resolveNumberToName(getApplicationContext(), username));

		dialog.setCancelable(true);

		Button okButton = (Button) dialog.findViewById(R.id.decryptedtext_ok_btn);
		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		String text = _sms.getMessage();
		try {
			text = Encrypter.decrypt(_sms.getMessage(), DBHandle.getInstance(getApplicationContext())
					.getOwnPrivateKey());
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), "Decryption failed", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
		TextView decryptedTextTv = (TextView) dialog.findViewById(R.id.decryptedtext_tv);
		decryptedTextTv.setText(text);

		dialog.show();

	}
}